#!/usr/bin/env bash

# 引入外部脚本
. "${LINUX_TOOLS_DIR}/utils/utils.sh"

# jdk下载
function download_jdk() {
  echo -e "\n=========================Download_JDK start=========================\n"
  local path=""
  read -r -p "请输入JDK保存的位置(save path)：" inputPath
  if [ "$inputPath" != "" ]; then
    path="-P "$inputPath
  fi

  while true; do
    echo -e "1: jdk1.7"
    echo -e "2: jdk1.8"
    echo -e "3: jdk11"
    echo -e "4: jdk15"
    echo -e "5: jdk17"
    echo -e "0: Exit"
    read -r -p "选择要下载的jdk版本：" input
    case $input in
    1)
      wget https://repo.huaweicloud.com/openjdk/java-jse-ri/jdk7u75/openjdk-7u75-b13-linux-x64-18_dec_2014.tar.gz $path
      exit
      ;;
    2)
      wget https://repo.huaweicloud.com/openjdk/java-jse-ri/jdk8u40/openjdk-8u40-b25-linux-x64-10_feb_2015.tar.gz $path
      exit
      ;;
    3)
      wget https://repo.huaweicloud.com/openjdk/java-jse-ri/jdk11/openjdk-11+28_linux-x64_bin.tar.gz $path
      exit
      ;;
    4)
      wget https://repo.huaweicloud.com/openjdk/15.0.2/openjdk-15.0.2_linux-x64_bin.tar.gz $path
      exit
      ;;
    5)
      wget https://repo.huaweicloud.com/openjdk/17.0.2/openjdk-17.0.2_linux-x64_bin.tar.gz $path
      exit
      ;;
    0)
      exit
      ;;
    *)
      echo_red "编号输入错误,请重新输入!"
      ;;
    esac

  done
  echo -e "\n=========================Download_JDK end=========================\n"

}

function mysql_backup() {
  if [[ "" == $(command -v mysql) ]]; then
    echo_red "当前主机未检测到Mysql服务,无法备份!"
    exit
  fi

  local DATE=$(date +%F_%H-%M-%S)
  local HOST=localhost
  local USER=backup
  local PASS=123.com
  local BACKUP_DIR=/data/mysql_backup

  read -r -p "请输入mysql主机ip：" inputHost
  if [ "$inputHost" != "" ]; then
    HOST=$inputHost
  fi
  read -r -p "请输入mysql的用户：" inputUser
  if [ "$inputUser" != "" ]; then
    USER=$inputUser
  fi
  read -r -p "请输入mysql的密码：" inputPass
  if [ "$inputPass" != "" ]; then
    PASS=$inputPass
  fi
  read -r -p "请输入mysql的备份路径(默认${BACKUP_DIR})：" inputBackUp
  if [ "$inputBackUp" != "" ]; then
    BACKUP_DIR=$inputBackUp
  fi
  DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null | egrep -v "Database|information_schema|mysql|performance_schema|sys|Warning")
  echo_red "查询到以下数据库可以备份:\n ${DB_LIST}"
  read -r -p "请输入要备份的数据库(不输入备份全部):" inputBackDb

  for DB in $DB_LIST; do
    BACKUP_DB_DIR=$BACKUP_DIR/${DB}_${DATE}
    [ ! -d $BACKUP_DB_DIR ] && mkdir -p $BACKUP_DB_DIR &>/dev/null

    if [ "$inputBackDb" == "" ]; then

      TABLE_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "use $DB;show tables;" 2>/dev/null)
      for TABLE in $TABLE_LIST; do
        BACKUP_NAME=$BACKUP_DB_DIR/${TABLE}.sql

        if ! mysqldump -h$HOST -u$USER -p$PASS $DB $TABLE >$BACKUP_NAME 2>/dev/null; then
          echo_red "$BACKUP_NAME 备份失败!"
        else
          echo_green "$BACKUP_NAME 备份成功!"
        fi
      done

    elif [ "${inputBackDb}" == ${DB} ]; then

      TABLE_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "use $DB;show tables;" 2>/dev/null)
      for TABLE in $TABLE_LIST; do
        BACKUP_NAME=$BACKUP_DB_DIR/${TABLE}.sql

        if ! mysqldump -h$HOST -u$USER -p$PASS $DB $TABLE >$BACKUP_NAME 2>/dev/null; then
          echo_red "$BACKUP_NAME 备份失败!"
        else
          echo_green "$BACKUP_NAME 备份成功!"
        fi
      done
    fi

  done

}

function port_scanning() {
  PORT_LIST="22"
  REMOTE_HOST=127.0.0.1
  TIMEOUT_SEC=1
  PORT_MAX=65535

  echo -e "\n============端口扫描============\n"
  echo -e "(1) 扫描本机所有开放端口 \n"
  echo -e "(2) 扫描指定端口  \n"
  read -r -p "请输入编号：" input

  case $input in
  1)
    for ((i = 1; i <= $PORT_MAX; i = i + 1)); do
      timeout $TIMEOUT_SEC bash -c "echo > /dev/tcp/$REMOTE_HOST/${i}" &>/dev/null
      res=$?
      if [ $res -eq 0 ]; then
        echo -e "\033[32m 服务端口:${REMOTE_HOST}:${i} 状态:OPEN \033[0m" #green
      fi
    done
    echo -e "\n所有端口扫描完毕!\n"

    ;;
  2)
    read -r -p "请输入要扫描的服务器ip:" inputIp
    REMOTE_HOST=$inputIp
    read -r -p "请输入要扫描的端口号(多个空格分隔):" inputPort
    PORT_LIST=$inputPort

    for PORT in $PORT_LIST; do
      timeout $TIMEOUT_SEC bash -c "echo > /dev/tcp/$REMOTE_HOST/$PORT" &>/dev/null
      res=$?
      if [ $res -eq 0 ]; then
        echo -e "\033[32m 服务端口:${REMOTE_HOST}:${PORT} 状态:OPEN \033[0m" #green
      elif [ $res -eq 1 ]; then
        echo -e "\033[31m 服务端口:${REMOTE_HOST}:${PORT} 状态:NOT LISTEN \033[0m" #red
      fi
    done

    ;;
  *)
    echo "编号输入错误,请重新输入!"
    ;;
  esac

}
